package com.goldenautumn.prefabricate.activity.mine.adapter

import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.view.ViewConfigurationCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.cart.SettlementGoodsAdapter
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.entity.OrderInfoVo
import com.goldenautumn.prefabricate.business.entity.RefundOrderInfoVo
import com.goldenautumn.prefabricate.databinding.ItemAfterSalesBinding

class AfterSalesAdapter :
    BaseQuickAdapter<RefundOrderInfoVo, DataBindingHolder<ItemAfterSalesBinding>>() {

    override fun onBindViewHolder(
        holder: DataBindingHolder<ItemAfterSalesBinding>,
        position: Int,
        item: RefundOrderInfoVo?
    ) {
        if (item == null) return
        var mBinding = holder.binding
        var params = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        when (holder.layoutPosition) {
            items.size - 1 -> params.setMargins(
                0,
                SizeUtils.dp2px(8f),
                0,
                SizeUtils.dp2px(16f)
            )

            else -> params.setMargins(
                0,
                SizeUtils.dp2px(8f),
                0,
                0
            )
        }
        holder.itemView.layoutParams = params
        var goodsAdapter = SettlementGoodsAdapter(isRefund = item.is_agree?.value)
        mBinding.goodsRv.layoutManager = LinearLayoutManager(context)
        mBinding.goodsRv.adapter = goodsAdapter

        mBinding.goodsRv.setOnTouchListener(object : View.OnTouchListener {

            override fun onTouch(v: View, event: MotionEvent): Boolean {
                if (event.action == MotionEvent.ACTION_UP) {
                    var scaledHorizontalScrollFactor =
                        ViewConfigurationCompat.getScaledHorizontalScrollFactor(
                            ViewConfiguration.get(context), context
                        )
                    var scaledVerticalScrollFactor =
                        ViewConfigurationCompat.getScaledVerticalScrollFactor(
                            ViewConfiguration.get(context), context
                        )
                    if (scaledHorizontalScrollFactor > 0 || scaledVerticalScrollFactor > 0) {
                        holder.itemView.performClick()
                        return true
                    }
                }
                return false
            }
        })

        mBinding.orderSn.text = String.format(context.getString(R.string.order_sn), "${item?.order_refund_id}")
        var goods = ArrayList<GoodsInfoVO>()
        goods.add(
            GoodsInfoVO(
                goods_image = item.order_goods?.image?.file_path,
                goods_name = item.order_goods?.goods_name,
                goods_sku = GoodsSkuVo(
                    goods_id = item.order_goods?.goods_id,
                    goods_attr = item.order_goods?.goods_attr,
                    goods_price = item.order_goods?.goods_price,
                    spec_sku_id = "${item?.order_refund_id}"
                ),
                total_num = item.order_goods?.total_num
            )
        )
        goodsAdapter.submitList(goods)

    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): DataBindingHolder<ItemAfterSalesBinding> {
        return DataBindingHolder(R.layout.item_after_sales, parent)
    }
}